<template>
  <div class="coupon-ccontainer">
    <el-form :model="searchParams" :inline="true" class="demo-form-inline" size="small">
      <el-form-item label="优惠券名称">
        <el-input v-model="searchParams.couponTemplateName" placeholder="请输入优惠券名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" icon="el-icon-search" @click="loadTable">查询</el-button>
        <el-button type="danger" size="small" icon="el-icon-refresh" @click="refresh">重置</el-button>
        <el-button type="success" size="small" icon="el-icon-plus" @click="addCoupon">添加优惠券</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData"
    	:default-sort="{ prop: 'createTime', order: 'descending' }"
    	style="width: 100%" border fit highlight-current-row>
    	<el-table-column fixed="left" align="center" label="序号" width="60">
    	  <template slot-scope="scope">
    	    {{(pageNum - 1) * pageSize + scope.$index + 1}}
    	  </template>
    	</el-table-column>
      <el-table-column prop="couponTemplateName" label="名称" width="250" show-overflow-tooltip></el-table-column>
      <el-table-column prop="couponType" label="类型" >
        <!-- 优惠券类型 -->
        <template slot-scope="{row}">
          <span v-if="row.type==0">满减券</span>
          <span v-if="row.type==1">折扣券</span>
        </template>
      </el-table-column>
      <el-table-column prop="useScope" label="适用范围">
        <!-- 优惠券领取期限 -->
        <template slot-scope="{row}">
          <span v-if="row.useScope==0">不限</span>
          <span v-else>{{row.goods.goodsName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="receiveDate" label="领取日期" width="220">
        <!-- 优惠券领取期限 -->
        <template slot-scope="{row}">
          <span v-if="row.receiveEndTime==null">不限时</span>
          <span v-else>{{row.receiveStartTime}}—{{row.receiveEndTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="useTime" label="有效使用时间" width="220">
        <!-- 优惠券使用有效天数 -->
        <template slot-scope="{row}">
          <span v-if="row.validStartTime==null&&row.validEndTime==null">{{row.validDay}}天</span>
          <span v-else>{{row.validStartTime}}—{{row.validEndTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="total" label="发布数量">
        <!-- 优惠券发行数量 -->
        <template slot-scope="{row}">
          <span v-if="row.total==0">不限量</span>
          <span v-else>
            <span style="color: #0a6aa1;">发布：{{row.total}}</span></br>
            <span style="color: red;">剩余：{{row.surplus}}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="couponState" label="状态" width="100">
        <!-- 优惠券状态 -->
        <template slot-scope="{row}">
          <el-tag size="mini" effect="dark" type="warning" v-if="row.state==0">关闭</el-tag>
          <el-tag size="mini" effect="dark" type="success" v-if="row.state==1">开启</el-tag>
          <el-tag size="mini" effect="dark" type="danger" v-if="row.state==-1">已删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" sortable width="150"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="{row}">
         <el-button v-if="row.state==1&&row.state!=-1" plain type="danger" size="mini" @click="closeState(row)">关闭</el-button>
         <el-button v-if="row.state==0&&row.state!=-1" plain type="success" size="mini" @click="openState(row)">开启</el-button>
         <el-button v-if="row.state==0" plain type="warning" size="mini" @click="copyBtn(row)">编辑</el-button>
         <el-button plain type="" size="mini" @click="infoBtn(row)">详情</el-button>
         <el-button v-if="row.state!=-1" plain type="danger" size="mini" @click="deleteBtn(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 15, 30, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <addCoupon ref="addCouponRef"></addCoupon>
    <infoCoupon ref="infoCouponRef"></infoCoupon>
  </div>
</template>

<script>
  import addCoupon from './add.vue'
  import infoCoupon from './info.vue'
  import {
    listCoupon,
    deleteCoupon,
    openCoupon,
    closeCoupon
  } from '@/api/mall.product.js'

  import {
    numFormat
  } from '@/utils/common'

  export default {
    components: {
      addCoupon,
      infoCoupon
    },
    data: function() {
      return {
        listLoading:false,
        pageNum:1,
        pageSize:10,
        total:0,
        tableData:[],
        // 查询
        searchParams: {
          couponTemplateName:undefined,
        }
      }
    },

    mounted() {
      // 加载优惠券列表
      this.loadCouponList();
    },
    methods: {

      // 加载数据
      async loadCouponList() {
        this.listLoading = true;
        var params = Object.assign(this.searchParams, {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        });
        var res = await listCoupon(params);
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.listLoading = false;
      },
      
      // 条件查询
      loadTable(){
        this.pageNum = 1
        this.loadCouponList();
      },
      // 查询
      searchTable() {
        this.loadCouponList();
      },
      // 重置
      refresh() {
        this.searchParams = {
          couponTemplateName:undefined,
        },
        this.searchTable();
      },

      // 改变每页显示的条数
      handleSizeChange(val) {
        this.pageSize = val;
        this.searchTable();
      },
      // 改变当前页
      handleCurrentChange(val) {
        this.pageNum = val;
        this.searchTable();
      },


      // 开启
      openState(row) {
        this.$confirm('确认开启?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(async () => {
          var res = await openCoupon(row.couponTemplateId)
          this.$utils.showSuccessMessage(res.msg);
          this.searchTable();
        });
      },

      // 关闭
      closeState(row) {
        this.$confirm('确认关闭?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(async () => {
          var res = await closeCoupon(row.couponTemplateId)
          this.$utils.showSuccessMessage(res.msg);
          this.searchTable();
        });
      },

      // 领取记录
      receiveLog(row){

      },

      // 复制
      copyBtn(row){
        this.$refs.addCouponRef.open(row.couponTemplateId)
      },

      // 详情
      infoBtn(row){
        this.$refs.infoCouponRef.open(row.couponTemplateId)
      },

      // 删除
      deleteBtn(row){
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(async () => {
          var res = await deleteCoupon(row.couponTemplateId);
          this.$utils.showSuccessMessage(res.msg);
          this.searchTable();
        });
      },

      // 添加优惠券
      addCoupon() {
        this.$refs.addCouponRef.open()
      },

    },
    // 过滤器 - 处理金额
    filters:{
      numFormatFilters(value){
        return numFormat(value)
      }
    }
  }

</script>

<style scoped lang="scss">
  .coupon-ccontainer {
    padding: 15px;
  }
</style>
